package com.shopnight.controller.rider;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopnight.entity.ErrandOrder;
import com.shopnight.entity.Rider;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.result.ApiResult;
import com.shopnight.service.AllOrderService;
import com.shopnight.service.ErrandOrderService;
import com.shopnight.service.RiderService;
import com.shopnight.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/token")
@Api(tags = {"riderController"},description = "骑手业务所需接口")
@Slf4j
public class riderController {

    @Autowired
    AllOrderService allOrderService;
    @Autowired
    ErrandOrderService errandOrderService;
    @Autowired
    RiderService riderService;

    @GetMapping("/getAllUnderwayOrder")
    @ApiOperation(value = "根据组织获取所有进行中订单", notes = "")
    public ApiResult getAllOrder(@RequestParam Integer orgId)  {
        return ApiResult.ok(allOrderService.getAllOrder(orgId));
    }
    @GetMapping("/getErrandOrderInfoByRider")
    @ApiOperation(value = "骑手查看订单详情接口", notes = "")
    public ApiResult getErrandOrderInfoByRider(@RequestParam Integer orderType,Integer OrderId)  {
        Integer userId = Integer.valueOf(UserContext.getEndUserId());
        Object result = null;
        switch (orderType) {
            case 2:
                // 查看跑腿订单详情
                Rider one = riderService.getOne(new LambdaQueryWrapper<Rider>().eq(Rider::getUserId, userId).eq(Rider::getState, 1));
                if (one == null) return  ApiResult.fail("您还不是接单员 请联系客服申请");
                //接单
                result = errandOrderService.getErrandOrderInfoByRider(OrderId);
                break;
            default:
                log.info("未知的订单类型");
                break;
        }
        return ApiResult.ok("",result);
    }


    @GetMapping("/receivingOrder")
    @ApiOperation(value = "骑手接单接口", notes = "")
    public ApiResult receivingOrder(@RequestParam Integer orderType,Integer OrderId)  {
        Integer userId = Integer.valueOf(UserContext.getEndUserId());

        switch (orderType) {

            case 2:
                // 处理跑腿订单
                Rider one = riderService.getOne(new LambdaQueryWrapper<Rider>().eq(Rider::getUserId, userId).eq(Rider::getState, 1));
                if (one == null) return  ApiResult.fail("您还不是接单员 请联系客服申请");
                //接单
                errandOrderService.receivingOrder(userId,OrderId);
                break;
            default:
                log.info("未知的订单类型");
                break;
        }
        return ApiResult.ok("接单成功！");
    }

    @GetMapping("/getRiderReceivingOrder")
    @ApiOperation(value = "获取骑手已接受的订单", notes = "")
    public ApiResult getRiderReceivingOrder(@RequestParam Integer orgId,Integer status)  {
        return ApiResult.ok(allOrderService.getRiderReceivingOrder(orgId,status));
    }

    @GetMapping("/riderCompleteOrder")
    @ApiOperation(value = "骑手完成订单接口", notes = "")
    public ApiResult riderCompleteOrder(@RequestParam Integer orderType,Integer OrderId)  {
        Integer userId = Integer.valueOf(UserContext.getEndUserId());
        switch (orderType) {
            case 2:
                // 处理跑腿订单
                Rider one = riderService.getOne(new LambdaQueryWrapper<Rider>().eq(Rider::getUserId, userId).eq(Rider::getState, 1));
                if (one == null) return  ApiResult.fail("您还不是接单员 请联系客服申请");
                //接单
                errandOrderService.riderCompleteOrder(userId,orderType,OrderId);
                break;
            default:
                log.info("未知的订单类型");
                break;
        }


        return ApiResult.ok("已完成！");
    }

    @GetMapping("/riderInfo")
    @ApiOperation(value = "骑手个人中心接口", notes = "")
    public ApiResult riderInfo()  {
        Integer userId = Integer.valueOf(UserContext.getEndUserId());
        Rider one = riderService.getOne(new LambdaQueryWrapper<Rider>().eq(Rider::getUserId, userId).eq(Rider::getState, 1));
        if (one == null) return  ApiResult.fail("您还不是接单员 请联系客服申请");

        return ApiResult.ok(riderService.riderInfo(userId));
    }



}
